#!/bin/bash

# Default installer program
default_install_program='all'
install_program_index_text="Enter serial number to select setup program (multiple separated by \`,\`) [${default_install_program}] :"

# All program indexes
program_index=''
for i in "${!programs[@]}"; do
  install_program_index_text="${install_program_index_text}\n$((1 + ${i})).${programs[${i}]}"
  program_index="${program_index}$((1 + ${i})),"
done
program_index=$(rstrip ${program_index} ',')

# Select setup program
echo -e ${install_program_index_text}
read install_program_index
until [[ -z ${install_program_index} || "${install_program_index}" =~ ^[0-9]+[,0-9]*$ ]]; do
  echo "${install_program_index}: invalid selection."
  echo -e ${install_program_index_text}
  read install_program_index
done
install_program_index=${install_program_index:-${default_install_program}}

# Install the program
if [ ${install_program_index} == ${default_install_program} ]; then
  install_program_index=${program_index}
fi
install_program=''
install_program_index_arr=$(remove_array_dups $(split ${install_program_index} ','))
for i in ${install_program_index_arr[@]}; do
  install_program="${install_program}${programs[$((${i} - 1))]},"
done
install_program=$(rstrip ${install_program} ',')

# Whether to install a program
[[ ${install_program} == *nginx* ]] && is_install_nginx='true' || is_install_nginx='false'
[[ ${install_program} == *mysql\(* ]] && is_install_mysql='true' || is_install_mysql='false'
[[ ${install_program} == *mongo* ]] && is_install_mongo='true' || is_install_mongo='false'
[[ ${install_program} == *redis* ]] && is_install_redis='true' || is_install_redis='false'
[[ ${install_program} == *rabbitmq* ]] && is_install_rabbitmq='true' || is_install_rabbitmq='false'
[[ ${install_program} == *php\(* ]] && is_install_php='true' || is_install_php='false'

# Alpine version
default_alpine_version='3.13'
if [ "${is_install_php}" == 'true' ]; then
echo "Please enter the php alpine version [${default_alpine_version}] :"
read alpine_version
fi
alpine_version=${alpine_version:-${default_alpine_version}}

# Install the PHP extension
default_install_php_extensions='bcmath,bz2,curl,exif,gd,gettext,intl,mbstring,mongodb,mysql,mysqli,opcache,pcntl,pdo_mysql,redis,sockets,zip'
if [ "${is_install_php}" == 'true' ]; then
  echo "Enter the php extension to install (multiple separated by \`,\`) [${default_install_php_extensions}] :"
  echo -e ${php_extensions}
  read install_php_extensions
fi
install_php_extensions=${install_php_extensions:-${default_install_php_extensions}}
if [ ${install_php_extensions} == 'all' ]; then
  install_php_extensions=${php_extensions}
fi

# Mysql password
if [ "${is_install_mysql}" == 'true' ]; then
  default_mysql_password=$(cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 12 | sed 's/[ \r\b]//g')
  echo "Please enter the mysql password [${default_mysql_password}] :"
  read mysql_password
  mysql_password=${mysql_password:-${default_mysql_password}}
fi

# Root directory
default_root_dir='wwwroot'
echo "Please enter the root directory name [${default_root_dir}] :"
read root_dir
root_dir=${root_dir:-${default_root_dir}}

# Whether overwrite existing files
default_is_force='n'
echo "Whether overwrite existing files [y/${default_is_force^}] :"
read is_force
is_force=${is_force:-${default_is_force}}
is_force=${is_force,,}

# Install remind
echo 'Install remind :'
length=100
str_repeat '=' ${length}
input_line 'Install the program              : '${install_program} ${length}
[ "${is_install_php}" == 'true' ] && input_line 'Install the PHP extension        : '${install_php_extensions} ${length}
input_line 'Whether overwrite existing files : '${is_force} ${length}
input_line 'Root directory                   : '${root_dir} ${length}
str_repeat '=' ${length}
echo

# Whether to install
default_is_install='y'
echo "Whether to start installation [${default_is_install^}/n] :"
read is_install
is_install=${is_install:-${default_is_install}}
is_install=${is_install,,}
